﻿using Application.Users.Queries.GetUser;
using Ardalis.Specification;
using Domain.Entities;

namespace Application.Users.Specifications
{
    public class UsersByFilterSpec : Specification<User>
    {
        public UsersByFilterSpec(GetUserFilter filter)
        {
            // 手机号（精确匹配）
            if (filter.PhoneNumbers?.Count > 0)
            {
                foreach (var number in filter.PhoneNumbers)
                    Query.Where(u => filter.PhoneNumbers.Contains(u.PhoneNumber));
            }

            // 姓名（模糊匹配）
            if (filter.UserNames?.Count > 0)
            {
                Query.Where(u => filter.UserNames.Contains(u.NickName));
            }

            // ID 列表
            if (filter.Ids?.Count > 0)
            {
                Query.Where(u => filter.Ids.Contains(u.Id));
            }

            // 学院名称列表
            if (filter.Colleges?.Count > 0)
            {
                Query.Where(u => u.AcademicInfo != null && filter.Colleges.Contains(u.AcademicInfo.CollegeName));
            }

            //专业名称列表
            if (filter.Majors?.Count > 0)
            {
                Query.Where(u => u.AcademicInfo != null && filter.Majors.Contains(u.AcademicInfo.MajorName));
            }

            //按创建时间排序
            Query.OrderBy(u => u.CreateTime);

            //分页
            Query.Skip(filter.PageSize * (filter.PageNo - 1)).Take(filter.PageSize);
        }
    }
}
